package com.naiterui.ehp.bs.recommend.controller.inner;

import com.naiterui.ehp.bp.bo.esign.PreSignResultBO;
import com.naiterui.ehp.bp.bo.recommend.param.RecomAuditParamBO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.recommend.bean.RecomAuditLog;
import com.naiterui.ehp.bs.recommend.exception.ExceptionCodes;
import com.naiterui.ehp.bs.recommend.service.IRecomAuditService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * @author guoyongxiang
 * @date 2019-06-18 19:41
 * @since 1.0.0
 */
@Validated
@Api(tags = {SwaggerTag.CMS + SwaggerTag.RECOM, SwaggerTag.INNER_API + SwaggerTag.RECOM,})
@RequestMapping("recommend/inner/recom/cms")
@RestController
public class InnerRecomCmsController {

    private static final Logger LOGGER = LoggerFactory.getLogger(InnerRecomCmsController.class);

    @Autowired
    private IRecomAuditService recomAuditService;

    /**
     * 药师签名预签(发送验证码)
     *
     * @param recomId
     * @param pharmacistId
     * @param operator
     *
     * @return
     *
     * @throws BusinessException
     * @throws UnsupportedEncodingException
     */
    @ApiOperation("药师签名预签(发送验证码)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recomId", value = "处方ID", paramType = "query", dataType = "long", required = true),
            @ApiImplicitParam(name = "pharmacistId", value = "药师ID", paramType = "query", dataType = "long",
                    required = true)})
    @PostMapping("preSign")
    public ResponseVO<PreSignResultBO> preSign(Long recomId, Long pharmacistId, @RequestHeader String operator)
            throws BusinessException,
            UnsupportedEncodingException {
        String oper = StringUtils.isEmpty(operator) ? "" : URLDecoder.decode(operator, "UTF-8");
        LOGGER.info("处方药师签名预签 recomId:{}, pharmacistId:{}, operator:{}", recomId, pharmacistId, oper);
        PreSignResultBO res = recomAuditService.preSignPh(recomId, pharmacistId);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, res);
    }

    @ApiOperation("处方药师签名")
    @PostMapping("sign")
    public ResponseVO<Void> sign(RecomAuditParamBO auditParamBO, @RequestHeader String operator) throws BusinessException,
            UnsupportedEncodingException {

        String oper = StringUtils.isEmpty(operator) ? "" : URLDecoder.decode(operator, "UTF-8");
        LOGGER.info("处方药师签名签名 signParamBO:{},  operator:{}", auditParamBO.toString(), oper);
        recomAuditService.pharmacistSign(auditParamBO, oper);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * 处方审核
     *
     * @param recomId
     * @param status
     * @param remark
     *
     * @return
     *
     * @throws BusinessException
     */
    @ApiOperation(value = "处方审核")
    @PostMapping("audit")
    public ResponseVO<Void> audit(RecomAuditParamBO auditParamBO, @RequestHeader String operator) throws BusinessException,
            UnsupportedEncodingException {

        if (RecomAuditLog.AUDIT_STATUS_REJECT == auditParamBO.getStatus() && StringUtils.isBlank(auditParamBO.getRemark())) {
            LOGGER.warn("审核不通过，原因必填 recomId:{}, status:{}", auditParamBO.getRecomId(), auditParamBO.getStatus());
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        String oper = StringUtils.isEmpty(operator) ? "" : URLDecoder.decode(operator, "UTF-8");
        recomAuditService.audit(auditParamBO, oper);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * 设置审核开关
     *
     * @param auditType
     * @param remark
     * @return
     * @throws BusinessException
     */
    @ApiOperation("设置审核开关")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "auditType", value = "审核类型(1:系统审核,2:人工审核)", paramType = "query", dataType = "int",
                    required = true),
            @ApiImplicitParam(name = "remark", value = "操作备注", paramType = "query", dataType = "string", required = true)})
    @PostMapping("audit/config")
    public ResponseVO<Void> auditConfig(Integer auditType, String remark, String operator) throws BusinessException {
        recomAuditService.auditConfig(auditType, remark, operator);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

}
